package com.example.login.view

import android.view.View
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.common.mvi.BaseMviActivity
import com.example.login.R
import com.example.login.databinding.ActivityLoginBinding
import com.example.login.entity.LoginValues
import com.example.login.intent.LoginViewIntent
import com.example.login.uistate.LoginUiState
import com.example.login.viewmodel.LoginViewModel
import com.example.path.RouterPath
import com.tencent.mmkv.MMKV
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@Route(path = RouterPath.PATH_LOGIN)
@AndroidEntryPoint
class LoginActivity : BaseMviActivity<ActivityLoginBinding>() {


    private val viewModel : LoginViewModel by viewModels()

    override fun getLayoutId(): Int = R.layout.activity_login
    override fun initViews() {

        //获取记录账号和密码
        var preAccount =MMKV.defaultMMKV().decodeString("pre_account","zxy")
        var prePwd =MMKV.defaultMMKV().decodeString("pre_pwd","123456")

        if (!(preAccount.isNullOrEmpty() && prePwd.isNullOrEmpty())){
            mDataBinding.includeLogin.cbRememberPwd.isChecked=true
            mDataBinding.includeLogin.edtLoginName.setText(preAccount)
            mDataBinding.includeLogin.edtLoginPwd.setText(prePwd)
        }


        //登录跳转到注册
        mDataBinding.includeLogin.tvRegister.setOnClickListener {
           showRegisterCardView()
        }

        mDataBinding.includeLogin.btnLogin.setOnClickListener {
          var userName= mDataBinding.includeLogin.edtLoginName.text.toString().trim()
          var userPwd= mDataBinding.includeLogin.edtLoginPwd.text.toString().trim()
            //跳转到主页面
            if(!(userName.isNullOrEmpty() && userPwd.isNullOrEmpty()) &&mDataBinding.cbAgree.isChecked){
                loginToServer(userName,userPwd)
                ARouter.getInstance().build(RouterPath.PATH_MAIN).navigation()
                this@LoginActivity.finish()
            }else{
                showToastMessage("请勾选同意用户协议")
            }

        }

        //注册返回登录
        mDataBinding.includeRegister.btnRegister.setOnClickListener {
            var userName= mDataBinding.includeRegister.edtRegisterName.text.toString().trim()
            var userPwd= mDataBinding.includeRegister.edtRegisterPwd.text.toString().trim()
            var usererPwd= mDataBinding.includeRegister.edtRegisteRePwd.text.toString().trim()
            if (!(userName.isNullOrEmpty() && userPwd.isNullOrEmpty()) && usererPwd==userPwd){
                registerToServer(userName,userPwd)
                showLoginCardView()
            }

        }

    }
    /**
     * 显示登录布局卡片
     */
    private fun showLoginCardView(){
        //隐藏登录布局卡片
        mDataBinding.cltLoginCard.visibility = View.VISIBLE
        //显示注册布局卡片
        mDataBinding.cltRegisterCard.visibility = View.INVISIBLE
        //隐藏用户协议
        mDataBinding.llUserAgreement.visibility = View.VISIBLE
    }

    /**
     *  显示注册布局卡片
     */
    private fun showRegisterCardView(){
        //隐藏登录布局卡片
        mDataBinding.cltLoginCard.visibility = View.INVISIBLE
        //显示注册布局卡片
        mDataBinding.cltRegisterCard.visibility = View.VISIBLE
        //隐藏用户协议
        mDataBinding.llUserAgreement.visibility = View.INVISIBLE
    }

    /**
     * 登录到服务器
     */
    private fun loginToServer(uName:String="zxy",Upwd:String="123456"){
        lifecycleScope.launch {
            viewModel.channel.send(LoginViewIntent.loginToServer(uName,Upwd))
        }
    }

    /**
     * 注册到服务器
     */
    private fun registerToServer(uName:String,Upwd:String){
        lifecycleScope.launch {
            viewModel.channel.send(LoginViewIntent.registerToServer(uName,Upwd))
        }
    }

    override suspend fun collectUIState() {
        viewModel.uiState.collect{
            when(it){
                is LoginUiState.LoginSuccess->handLoginSuccess(it.data)
                is LoginUiState.LoginFail->showToastMessage(it.msg)
                is LoginUiState.registerSuccess->handRegisterSuccess(it.data)
                is LoginUiState.registerFail->showToastMessage(it.msg)
                else->{}
            }
        }
    }



    private fun handLoginSuccess(data: Any) {
        var entity = data as LoginValues
        //判断是否选中记录密码
        if (mDataBinding.includeLogin.cbRememberPwd.isChecked){
            MMKV.defaultMMKV().encode("pre_account",entity.uName)
            MMKV.defaultMMKV().encode("pre_pwd",entity.uPwd)
        }


    }
    private fun handRegisterSuccess(data: Any) {
        var entity =data as LoginValues
        mDataBinding.includeLogin.edtLoginName.setText(entity.uName)
        mDataBinding.includeLogin.edtLoginPwd.setText(entity.uPwd)
        showToastMessage("注册成功")
        showLoginCardView()
    }


}